package com.cmy.framework_widget

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator


/**
 * @name:
 *  LoadingView
 * @package:
 *  com.cmy.framework_widget
 * @date:
 *  2023/5/1617:58
 * @author
 *    cmy
 * @description
 *  显示Loading对应的UI
 */
class LoadingView : View {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0):super( context, attrs ,defStyleAttr)

    private var mAngle = 0f

    init {
        init()
    }

  @SuppressLint("ObjectAnimatorBinding")
  private  fun init(){
        // 设置一个周期为1秒的旋转动画
        // 设置一个周期为1秒的旋转动画
        val animator = ObjectAnimator.ofFloat(this, "angle", 0f, 360f)
        animator.duration = 1000
        animator.interpolator = LinearInterpolator()
        animator.repeatCount = ValueAnimator.INFINITE
        animator.start()
    }


    /**
     * 设置透明度
     */
    fun setAngle(angle:Float){
        mAngle = angle
        invalidate()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val centerX = width / 2
        val centerY = height / 2
        val radius = centerX.coerceAtMost(centerY) - 10

        val paint = Paint()
        paint.color = Color.parseColor("#FF4081")
        paint.style = Paint.Style.FILL
        canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)

        paint.color = Color.WHITE
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5F
        canvas.save()
        canvas.rotate(mAngle, centerX.toFloat(), centerY.toFloat())
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
        canvas.restore()
    }
}